package com.wuzihao.reggie.controller;

import com.wuzihao.reggie.common.R;
import com.wuzihao.reggie.entity.User;
import com.wuzihao.reggie.service.UserService;
import com.wuzihao.reggie.utils.SmsUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("/sendMsg")
    public R sendMsg(@RequestBody User user, HttpSession session){
        if(StringUtils.isNotEmpty(user.getPhone())){
            userService.sendSms(user,session);

            return R.success("短信发送成功");
        }
        return R.error("手机号不能为空");
    }

    @PostMapping("/login")
    public R login(@RequestBody Map map, HttpSession session){
        User user = userService.login(map,session);
        if(user == null){
            return R.error("登陆失败");
        }
        return R.success("登录成功");
    }
}
